package ccm.component.override;

import ccm.CCMonitor;
import ccm.common.CCMConfigManager;
import ccm.common.SystemParametersInfo;
import ccm.component.AboutJDialog;
import ccm.tools.GenerateFont;
import ccm.tools.GenerateImage;
import ccm.tools.ImageViewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

abstract public class JFrame extends javax.swing.JFrame
{
    private static final String allTitle="车车Monitor";
    protected final JMenuBar jMenuBar;
    protected final JMenu setJMenu;
    protected final JMenu toolsJMenu;
    protected final JMenu helpJMenu;

    public JFrame() throws HeadlessException
    {
        this(getTitleString(""));
    }

    public JFrame(boolean toolsMenu) throws HeadlessException
    {
        this(getTitleString(""),toolsMenu);
    }

    public JFrame(String title) throws HeadlessException
    {
        this(title,false);
    }

    public JFrame(String title,boolean toolsMenu) throws HeadlessException
    {
        super(getTitleString(title));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        System.out.println(this.getClass().getName());
        setSize(Math.max(100,CCMConfigManager.setIntegerIfAbsent(this.getClass().getName()+"/sizeX",1400)),Math.max(100,CCMConfigManager.setIntegerIfAbsent(this.getClass().getName()+"/sizeY",900)));
        setExtendedState(CCMConfigManager.setIntegerIfAbsent(this.getClass().getName()+"/extended",JFrame.NORMAL));

        jMenuBar=new JMenuBar();
        setJMenuBar(jMenuBar);

        setJMenu=new JMenu("设置");
        jMenuBar.add(setJMenu);
        JCheckBoxMenuItem top=new JCheckBoxMenuItem("置顶");
        setJMenu.add(top);
        top.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
        top.setSelected(CCMConfigManager.setBooleanIfAbsent(this.getClass().getName()+"/top",false));
        setAlwaysOnTop(CCMConfigManager.setBooleanIfAbsent(this.getClass().getName()+"/top",false));
        top.addActionListener(e->
        {
            setAlwaysOnTop(top.isSelected());
            CCMConfigManager.setBoolean(this.getClass().getName()+"/top",top.isSelected());
        });

        JMenuItem openConfigDir=new JMenuItem("打开配置文件目录");
        setJMenu.add(openConfigDir);
        openConfigDir.addActionListener(e->
        {
            try
            {
                Runtime.getRuntime().exec("explorer.exe /e,/select,"+CCMConfigManager.getConfigFile().replace('/','\\'));
            }catch(IOException ignored)
            {
            }
        });
        JMenuItem restartJMenuItem=new JMenuItem("重启");
        setJMenu.add(restartJMenuItem);
        restartJMenuItem.addActionListener(e->restart());
        JMenuItem setFont=new JMenuItem("设置字体");
        setJMenu.add(setFont);
        setFont.addActionListener(e->
        {
            JDialog jDialog=new JDialog(this,"设置字体(重启有效)",true);
            JPanel jPanel=new JPanel();
            jPanel.setLayout(new VFlowLayout());
            jDialog.setContentPane(jPanel);
            jDialog.setSize(600,400);
            Point lu=getLocationOnScreen();
            Dimension ps=getSize();
            Dimension ms=jDialog.getSize();

            {
                final JPanel panel1=new JPanel();
                jPanel.add(panel1);
                panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
                panel1.add(new JLabel("字体:"));
                final JComboBox<String> fontTypeJComboBox=new JComboBox<String>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
                panel1.add(fontTypeJComboBox);
                fontTypeJComboBox.setSelectedItem(CCMConfigManager.getFont("ccm/general/font").getName());

                final JPanel panel2=new JPanel();
                jPanel.add(panel2);
                panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
                panel2.add(new JLabel("字号:"));
                final JSpinner fontSizeJSPinner=new JSpinner(new SpinnerNumberModel(CCMConfigManager.getFont("ccm/general/font",false).getSize(),0,200,1));
                panel2.add(fontSizeJSPinner);
                panel2.add(new JLabel("*"+SystemParametersInfo.getFontRate()));

                fontTypeJComboBox.addActionListener(e1->CCMConfigManager.setFont("ccm/general/font",new Font((String)fontTypeJComboBox.getSelectedItem(),Font.PLAIN,(Integer)fontSizeJSPinner.getValue())));
                fontSizeJSPinner.addChangeListener(e1->CCMConfigManager.setFont("ccm/general/font",new Font((String)fontTypeJComboBox.getSelectedItem(),Font.PLAIN,(Integer)fontSizeJSPinner.getValue())));
            }
            final JButton jButton2=new JButton("确定并重启");
            jPanel.add(jButton2);
            jButton2.addActionListener(e12->
            {
                CCMConfigManager.save();
                restart();
            });
            jDialog.setLocation((ps.width>ms.width)?(((ps.width-ms.width)/2)+lu.x):(lu.x),(ps.height>ms.height)?(((ps.height-ms.height)/2)+lu.y):(lu.y));
            jDialog.setVisible(true);
        });
        if(toolsMenu)
        {
            toolsJMenu=new JMenu("工具");
            jMenuBar.add(toolsJMenu);
            JMenuItem t1=new JMenuItem("GenerateImage");
            toolsJMenu.add(t1);
            t1.addActionListener(e->new GenerateImage().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE));
            JMenuItem t2=new JMenuItem("ImageViewer");
            toolsJMenu.add(t2);
            t2.addActionListener(e->new ImageViewer().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE));
            JMenuItem t3=new JMenuItem("GenerateFont");
            toolsJMenu.add(t3);
            t3.addActionListener(e->new GenerateFont().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE));
            JMenuItem t4=new JMenuItem("CCMonitor");
            toolsJMenu.add(t4);
            t4.addActionListener(e->
            {
                try
                {
                    new CCMonitor().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                }catch(Exception ex)
                {
                    JOptionPane.showMessageDialog(null,ex.getMessage(),"启动失败",JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            });

        }
        else
        {
            toolsJMenu=null;
        }
        {
            helpJMenu=new JMenu("帮助");
            jMenuBar.add(helpJMenu);
            JMenuItem aboutJMenuItem=new JMenuItem("关于");
            helpJMenu.add(aboutJMenuItem);
            aboutJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
            aboutJMenuItem.addActionListener(e->new AboutJDialog(this).setVisible(true));
        }
        addComponentListener(new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent e)
            {
                CCMConfigManager.setInteger(JFrame.this.getClass().getName()+"/extended",getExtendedState());
                if(getExtendedState()==NORMAL)
                {
                    CCMConfigManager.setInteger(JFrame.this.getClass().getName()+"/sizeX",getSize().width);
                    CCMConfigManager.setInteger(JFrame.this.getClass().getName()+"/sizeY",getSize().height);
                }
            }
        });
    }

    /**
     * 获取标题的字符串
     *
     * @param title 标题
     * @return 标题的字符串
     */
    static public String getTitleString(String title)
    {
        return (title.isEmpty())?(allTitle):(allTitle+"|"+title);
    }


    public JFrame(String[] title) throws HeadlessException
    {
        this(getTitleString(title));
    }

    /**
     * 获取标题的字符串
     *
     * @param title 标题
     * @return 标题的字符串
     */
    static public String getTitleString(String[] title)
    {
        StringBuilder ans=new StringBuilder(allTitle);
        for(String s: title)
        {
            ans.append("-").append(s);
        }
        return ans.toString();
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title)
    {
        super.setTitle(getTitleString(title));
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String[] title)
    {
        super.setTitle(getTitleString(title));
    }

    abstract public void restart();
}
